package cn.tedu.product.controller;

import com.netflix.discovery.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
public class CodeController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    //发送验证码
    //接受提交判断是否正确
    @RequestMapping("/sendCode")
    public String sendCode(String phone) {
        int code = new Random().nextInt(9999);
        //验证手机号
        System.out.println("给"+phone+"发送了验证码");
        String key=phone+".code";

        redisTemplate.opsForValue().set(key,code+"",15, TimeUnit.MINUTES);
        return "成功发送";
    }
    @RequestMapping("/verify")
    public String verify(String phone,String code) {
        String key = phone + ".code";
        if(redisTemplate.hasKey(key)){
            String codeValue =redisTemplate.opsForValue().get(key);
            if(StringUtils.isNotEmpty(code)&&code.equals(codeValue)){
                System.out.println("提交成功");
            }
        }else {
            System.out.println("手机号输入有误，没有发送验证码");
        }
        return "验证完毕";
    }
}
